package com.emaimed.search.es;

import com.emaimed.search.pojo.EmaimedSms;
import com.emaimed.search.pojo.PageInfo;
import io.searchbox.client.JestClient;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author tuofafa
 * @version 1.0
 * @date 2021/6/24 0024 11:28
 */
@Service
public class SmsDataEs {

    @Autowired
    private JestClient jestClient;

    public Map<String,Object> getEmaimedSmsAll(PageInfo pageInfo) throws IOException {

        Map<String,Object> map = new HashMap<>();

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();

        searchSourceBuilder.size(pageInfo.getPageSize());
        searchSourceBuilder.from((pageInfo.getCurrentPage()-1)*pageInfo.getPageSize());
        SearchSourceBuilder query = searchSourceBuilder.query(boolQueryBuilder);

        Search search = new Search.Builder(query.toString()).addIndex("smsdata").addType("MessageData").build();
        SearchResult execute = jestClient.execute(search);

        List<EmaimedSms> emaimedSmsList = new ArrayList<>();

        List<SearchResult.Hit<EmaimedSms, Void>> hits = execute.getHits(EmaimedSms.class);
        for (SearchResult.Hit<EmaimedSms,Void> hit:hits) {
            EmaimedSms emaimedSms = hit.source;
            emaimedSmsList.add(emaimedSms);
        }

        map.put("emaimedSmsList",emaimedSmsList);

        pageInfo.setTotal(execute.getTotal().intValue());
        map.put("pageInfo",pageInfo);

        return map;
    }

    /***
     * 根据发送方号码来检索
     * @return
     */
    public Map<String,Object> getSendNumbers(PageInfo pageInfo, String param) throws IOException {

        Map<String,Object> map = new HashMap<>();

        //使用dsl工具查询
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //bool
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        //must
        MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("sendNumber",param);
        boolQueryBuilder.must(matchQueryBuilder);

        //query
        SearchSourceBuilder query = searchSourceBuilder.query(boolQueryBuilder);
        searchSourceBuilder.size(pageInfo.getPageSize());
        searchSourceBuilder.from((pageInfo.getCurrentPage()-1)*pageInfo.getPageSize());

        Search search = new Search.Builder(query.toString()).addIndex("smsdata").addType("MessageData").build();
        SearchResult execute = jestClient.execute(search);

        List<EmaimedSms> emaimedSmsList = new ArrayList<>();

        List<SearchResult.Hit<EmaimedSms, Void>> hits = execute.getHits(EmaimedSms.class);
        for (SearchResult.Hit<EmaimedSms,Void> hit:hits) {
            EmaimedSms emaimedSms = hit.source;
            emaimedSmsList.add(emaimedSms);
        }

        map.put("emaimedSmsList",emaimedSmsList);

        pageInfo.setTotal(execute.getTotal().intValue());
        map.put("pageInfo",pageInfo);

        return map;
    }

    /***
     * 根据接收方号码来检索
     * @return
     */
    public Map<String, Object> getReceiveNumber(PageInfo pageInfo, String param) throws IOException {

        Map<String,Object> map = new HashMap<>();

        //使用dsl工具查询
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //bool
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        //must
        MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("receiveNumber",param);
        boolQueryBuilder.must(matchQueryBuilder);

        //query
        SearchSourceBuilder query = searchSourceBuilder.query(boolQueryBuilder);
        searchSourceBuilder.size(pageInfo.getPageSize());
        searchSourceBuilder.from((pageInfo.getCurrentPage()-1)*pageInfo.getPageSize());

        Search search = new Search.Builder(query.toString()).addIndex("smsdata").addType("MessageData").build();
        SearchResult execute = jestClient.execute(search);

        List<EmaimedSms> emaimedSmsList = new ArrayList<>();

        List<SearchResult.Hit<EmaimedSms, Void>> hits = execute.getHits(EmaimedSms.class);
        for (SearchResult.Hit<EmaimedSms,Void> hit:hits) {
            EmaimedSms emaimedSms = hit.source;
            emaimedSmsList.add(emaimedSms);
        }

        map.put("emaimedSmsList",emaimedSmsList);

        pageInfo.setTotal(execute.getTotal().intValue());
        map.put("pageInfo",pageInfo);

        return map;
    }


    /***
     * 根据短信内容来检索
     * @return
     */
    public Map<String, Object> getContent(PageInfo pageInfo, String param) throws IOException {
        Map<String,Object> map = new HashMap<>();

        //使用dsl工具查询
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //bool
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        //must
        MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("content",param);
        boolQueryBuilder.must(matchQueryBuilder);

        //query
        SearchSourceBuilder query = searchSourceBuilder.query(boolQueryBuilder);
        searchSourceBuilder.size(pageInfo.getPageSize());
        searchSourceBuilder.from((pageInfo.getCurrentPage()-1)*pageInfo.getPageSize());

        Search search = new Search.Builder(query.toString()).addIndex("smsdata").addType("MessageData").build();
        SearchResult execute = jestClient.execute(search);

        List<EmaimedSms> emaimedSmsList = new ArrayList<>();

        List<SearchResult.Hit<EmaimedSms, Void>> hits = execute.getHits(EmaimedSms.class);
        for (SearchResult.Hit<EmaimedSms,Void> hit:hits) {
            EmaimedSms emaimedSms = hit.source;
            emaimedSmsList.add(emaimedSms);
        }

        map.put("emaimedSmsList",emaimedSmsList);

        pageInfo.setTotal(execute.getTotal().intValue());
        map.put("pageInfo",pageInfo);

        return map;
    }


}
